with managed;
with managed.Logger;
with managed.Alarm;

with GUI.Instances;

separate(ffxialarm)
package body Tasks is
  package body Logger_Routines is separate;
  package body Alarm_Routines is separate;

  procedure Stop_Logger is
    use managed.Logger;
  begin
    Command.Set(Stop);
  end Stop_Logger;

  procedure Start_Stop_Logger is
    use managed.Logger;
    use GUI.Instances;
    Process : Process_ID;
    Success : Boolean;
  begin
    if Command.Retrieve = Stop then
      Choose_Instance(Main_Window, Process, Success);
      if Success then
        Logger.Start(Process);
      end if;
    else
      Command.Set(Stop);
    end if;
  end Start_Stop_Logger;

  task body Logger is
    use Logger_Routines;
    use managed.Logger;
    Step_delay : constant duration := 0.5;
    Par        : Parameters;
    Success    : Boolean;
    Process    : Process_ID;
  begin
    loop
      select
        accept Start(Process : in Process_ID) do
          Logger.Process := Process;
        end Start;
      or
        terminate;
      end select;
      Start(Process, Par, Success);
      if Success then
        Command.Set(Log);
        loop
          Work(Par, Success);
          if Command.Retrieve = Log then
            delay Step_delay;
          else
            exit;
          end if;
        end loop;
        if Success then
          Finish(Par);
        end if;
      end if;
    end loop;
  end Logger;

  procedure Play(Which : in Integer) is
    use managed.Alarm;
  begin
    if Which > -1 then
      Command.Set_Alarm(Which);
      if Command.Retrieve = stop then
        Alarm.Start;
      end if;
    end if;
  end Play;

  task body Alarm is
    use Alarm_Routines;
    use managed.Alarm;
  begin
    loop
      select
        accept Start;
      or
        terminate;
      end select;
      Command.Set(Play);
      Play_and_Repeat;
      Command.Set(Stop);
    end loop;
  end Alarm;
end Tasks;
